<?php
namespace app\command;


use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use app\common\service\GuguService;


class PlanByprovince extends Command
{
    protected function configure()
    {
        $this->setName('plan_byprovince')->setDescription('Your command description');
    }

    protected function execute(Input $input, Output $output)
    {
        
        
		$list = Db::name('area')->field('id,name')->where('pid',0)->where('is_plan_update',0)->order('id asc')->select();
		foreach($list as $key=>$province){
			$process = new \Swoole\Process(function () use ($province) {
               $this->start_task($province);;
            }, false);
            $process->start();
		}
		
        // 等待所有子进程完成
        while ($status = \Swoole\Process::wait(true)) {
            // 子进程退出
            //echo "Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}" . PHP_EOL;
            echo "子进程 {$status['pid']}退出\n";
        }
        echo "父进程 ".getmypid()."退出\n";
		setLog('plan_byprovince','执行完毕：');exit();
		
		
    }
	
	protected function start_task($task){
		
		echo "子进程 ".getmypid()."处理任务".$task['name']." \n";
		
		$page = 1;
		$result = GuguService::make('/metadata/college-enrollment-plan',['appkey'=>'4PRZZRKKA2QXJNHUF3222XWKCNTW7UYC','pageindex'=>$page,'pagesize'=>100,'provincename'=>$task['name'],'year'=>'2022']);
		if($result[0] != 200){
		   setLog('plan_byprovince','没有数据1：'.$task['name']);
		  
		}else{
			$page_total = ceil($result[1][0]['DataTotalCount']/100);
			for($page=1;$page<=$page_total;$page++){
				$result = GuguService::make('/metadata/college-enrollment-plan',['appkey'=>'4PRZZRKKA2QXJNHUF3222XWKCNTW7UYC','pageindex'=>$page,'pagesize'=>100,'provincename'=>$task['name'],'year'=>'2022']);
				$dataM = [];
				$nowtime = time();
				if(!empty($result[1][1])){
					foreach($result[1][1] as $k=>$v){
						$dataM[] = ['province_id'=>$task['id'],'year'=>$v['Year'],'province_name'=>$v['ProvinceName'],'in_school_years'=>$v['InSchoolYears'],'class_one'=>$v['ClassOne'],'class_two'=>$v['ClassTwo'],'batch_name'=>$v['BatchName'],'type'=>$v['Type'],'school_name'=>$v['SchoolName'],'enrollment_numbers'=>$v['EnrollmentNumbers'],'school_uuid'=>$v['SchoolUUID'],'course_selection_requirements'=>$v['CourseSelectionRequirements'],'college_major_name'=>$v['CollegeMajorName'],'college_major_code'=>$v['CollegeMajorCode'],'special_group'=>$v['SpecialGroup'],
						'special_code'=>$v['SpecialCode'],'special_group_name'=>$v['SpecialGroupName'],
						'create_time'=>$nowtime];
					}
				}
				if(!empty($dataM)){
					$flag = Db::connect(config('database'))->table('think_school_plan_new')->insertAll($dataM);
					if(!$flag){
						setLog('plan_byprovince','插入出错：'.$task['name'].$page.'页');
					}
				}else{
					setLog('plan_byprovince','没有数据2：'.$task['name']);
				}
			}
		}
		Db::connect(config('database'))->table('think_area')->where('id',$task['id'])->update(['is_plan_update'=>1]);
		setLog('plan_byprovince','拉取成功：'.$task['name']);
		
		echo "子进程 ".getmypid()."完成任务".$task['name']." \n";
	}

}
